package cn.csc.module.product.mybatis;

import cn.csc.module.product.controller.admin.spu.vo.SpuBasicAttrReqV2VO;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * @author 云隐
 * @date 2024/11/22
 **/
@Slf4j
@Component
@MappedTypes({Object.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class SpuBasicTypeHandler extends AbstractJsonTypeHandler<Object> {

    @Autowired
    ObjectMapper objectMapper;

    @Override
    protected Object parse(String json) {
        try {
            // 这里进行了json解析，同样在这里也可以进行字段查询后的处理
            return objectMapper.readValue(json, new TypeReference<List<SpuBasicAttrReqV2VO>>(){});
        } catch (IOException var3) {
            log.error("SpuBasicTypeHandler.parse:转换失败{}",var3);
            throw new RuntimeException(var3);
        }

    }

    @Override
    protected String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException var3) {
            log.error("SpuBasicTypeHandler.toJson:转换失败{}",var3);
            throw new RuntimeException(var3);
        }
    }
}
